<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'店铺类目'),
);
//商品类目
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['f_cat_name'])){
		$params['f_cat_name like']='%'.trim($_RQ['f_cat_name']).'%';
		$return['params']['f_cat_name']=$_RQ['f_cat_name'];
	}
	if(!empty($_RQ['second_cat_name'])){
		$params['second_cat_name like']='%'.trim($_RQ['second_cat_name']).'%';
		$return['params']['second_cat_name']=$_RQ['second_cat_name'];
	}
	if(!empty($_RQ['f_cat_id'])){
		$params['f_cat_id']=$_RQ['f_cat_id'];
		$return['params']['f_cat_id']=$_RQ['f_cat_id'];
	}
	if(!empty($_RQ['second_cat_id'])){
		$params['second_cat_id']=$_RQ['second_cat_id'];
		$return['params']['second_cat_id']=$_RQ['second_cat_id'];
	}
	if(!empty($_RQ['qualification_type'])){
		$params['qualification_type']=$_RQ['qualification_type'];
		$return['params']['qualification_type']=$_RQ['qualification_type'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_kuaidian_cate',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){

		}
	}	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'
		全部的一、二、三级类目信息、类目的资质信息、商品资质信息
		</br><red>同步类目后，要刷新一下《一二类目》，这样发布商品，才能选择到一二类目</red>；
		','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'类目名称','type'=>'string'),
			array('name'=>'cat_id','title'=>'类目ID','type'=>'string'),
			array('name'=>'f_cat_name','title'=>'一级类目','type'=>'string'),
			array('name'=>'f_cat_id','title'=>'一级类目ID','type'=>'string'),
			array('name'=>'second_cat_name','title'=>'二级类目','type'=>'string'),
			array('name'=>'second_cat_id','title'=>'二级类目ID','type'=>'string'),
			array('name'=>'qualification','title'=>'类目资质','type'=>'string'),
			array('name'=>'qualification_type','title'=>'类目资质类型','type'=>'radio','options'=>'qualification_type'),
			//array('name'=>'product_qualification','title'=>'商品资质','type'=>'string'),
			array('name'=>'product_qualification_type','title'=>'商品资质类型','type'=>'radio','options'=>'qualification_type'),
		),'batch'=>array(
			array('type'=>'post','url'=>'/category/updata/','title'=>'同步类目'),
			array('type'=>'post','url'=>'/category/upcates/','title'=>'刷新一二类目'),
			array('type'=>'post','url'=>'/category/delall/','title'=>'清空类目'),
			array('type'=>'post','url'=>'/category/up_brand/','title'=>'同步品牌'),
		),'action'=>array(
			array('type'=>'href','op'=>'checkup','title'=>'申请资质','show'=>array('qualification_type'=>1)),
			array('type'=>'modal','op'=>'lookaudit','title'=>'审核结果','show'=>array('qualification_type'=>1)),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'类目名称','type'=>'string'),
			array('name'=>'f_cat_name','width'=>10,'title'=>'一级类目','type'=>'string'),
			array('name'=>'f_cat_id','width'=>10,'title'=>'一级类目ID','type'=>'string'),
			array('name'=>'second_cat_name','width'=>10,'title'=>'二级类目','type'=>'string'),
			array('name'=>'second_cat_id','width'=>10,'title'=>'二级类目ID','type'=>'string'),
			array('name'=>'qualification_type','width'=>10,'title'=>'类目资质类型','type'=>'radio','options'=>'qualification_type'),
		)
	);
	$return['htmls']=$htmls;
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['qualification_type']=$this->qualification_type;
	$return['status']=$status;
	exi($return);
}
if($_RQ['op']=='delall'){
	$this->delete('cate',array('uniacid'=>$_SESSION['uniacid']));
	exi('清空成功！');
}
if($_RQ['op']=='updata'){
	$this->model('goods')->upcate();
	exi('同步类目成功！');
}
if($_RQ['op']=='upcates'){
	$catesall=pdo_getall('jy_kuaidian_cate',array('second_cat_id >'=>0,'f_cat_id >'=>0,'uniacid'=>$_SESSION['uniacid']),'','id');
	foreach($catesall as &$gv){
		if($gv['f_cat_id']){
			$isadd=pdo_getcolumn('jy_kuaidian_cate',array('cat_id'=>$gv['f_cat_id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$isadd){
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>trim($gv['f_cat_name']),
					'cat_id'=>intval($gv['f_cat_id']),
					'f_cat_id'=>0,
					'f_cat_name'=>'',
				);
				pdo_insert('jy_kuaidian_cate',$data);
			}
		}
		if($gv['second_cat_id']){
			$isadd=pdo_getcolumn('jy_kuaidian_cate',array('cat_id'=>$gv['second_cat_id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$isadd){
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>trim($gv['second_cat_name']),
					'cat_id'=>intval($gv['second_cat_id']),
					'f_cat_id'=>$gv['f_cat_id'],
					'f_cat_name'=>trim($gv['f_cat_name']),
				);
				pdo_insert('jy_kuaidian_cate',$data);
			}
		}
	}
	exi('刷新类目成功！');
}
if($_RQ['op']=='checkup'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		//$post['level3']=pdo_getcolumn('jy_kuaidian_cate',array('id'=>$post['level3']),'cat_id');
		if(!$post['level3']){
			//exi('请选择类目！','error');
		}
		if(!$post['license']){
			exi('请上传营业执照或组织机构代码证！','error');
		}
		if(!$post['certificate']){
			exi('请上传资质材料！','error');
		}
		$post['category_info']['level1']=$post['f_cat_id'];
		$post['category_info']['level2']=$post['second_cat_id'];
		$post['category_info']['level3']=$post['cat_id'];
		$post['category_info']['certificate']=$post['certificate'];
		$post['category_info']['license']=$post['license'];
		$audit_id=$this->model('wxapp_api')->audit_category($post['category_info']);
		$post['category_info']['audit_id']=$audit_id;
		$update=array(
			'category_info'=>ijson_encode($post['category_info']),
		);
		pdo_update('jy_kuaidian_cate',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('提交成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('cate',array('id'=>$id));
	$item['category_info']=json_decode($item['category_info'],true);
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'申请资质',
	);
	$htmls['edit']=array(
			array('name'=>'f_cat_name','title'=>'一级类目','type'=>'copy','help'=>''),
			array('name'=>'second_cat_name','title'=>'二级类目','type'=>'copy','help'=>''),
			array('name'=>'title','title'=>'三级类目','type'=>'copy','help'=>''),
			array('name'=>'license','title'=>'营业执照或组织机构代码证','type'=>'images','help'=>''),
			array('name'=>'certificate','title'=>'资质材料','type'=>'images','help'=>''),
	);
	$return['htmls']=$htmls;
	/*$cates=$this->getall('cate',array('second_cat_id'=>0,'f_cat_id'=>0,'uniacid'=>$_SESSION['uniacid']),array('cat_id','title'),'cat_id');
	foreach($cates as &$c1){
		$c1['id']=$c1['cat_id'];
		$c1['cate_2']=$this->getall('cate',array('second_cat_id'=>0,'f_cat_id'=>$c1['cat_id'],'uniacid'=>$_SESSION['uniacid']),array('cat_id','title'),'cat_id');
		foreach($c1['cate_2'] as &$c2){
			$c2['id']=$c2['cat_id'];
			$c2['cate_3']=$this->getall('cate',array('second_cat_id'=>$c2['cat_id'],'uniacid'=>$_SESSION['uniacid']),array('id','cat_id','title'),'id');
		}
	}
	$return['cates']=$cates;*/
	exi($return);
}
if($_RQ['op']=='lookaudit'){
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('cate',array('id'=>$id));
	$item['category_info']=json_decode($item['category_info'],true);
	$audit_id=$item['category_info']['audit_id'];
	if(!$audit_id){
		exi('您还未提交申请吧！','error');
	}	
	if($item['category_info']['status']!=1){
		$res=$this->model('wxapp_api')->lookaudit($audit_id);
		$item['category_info']['status']=$res['status'];
		$update=array(
			'category_info'=>ijson_encode($item['category_info']),
		);
		pdo_update('jy_kuaidian_cate',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$res=array(
			'status'=>1,
			'reject_reason'=>'审核通过',
		);
	}
	$return['item']=$res;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'申请资质结果',
	);
	$htmls['look']=array(
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'status'),
			array('name'=>'reject_reason','title'=>'审核详情','type'=>'string','help'=>''),
	);
	$return['htmls']=$htmls;
	$return['status']=array('审核中','审核成功','审核拒绝');
	exi($return);
}
if($_RQ['op']=='up_brand'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'f_cat_id >'=>0);
	$list=pdo_count('jy_kuaidian_cate',$params);
	if(!$list){
		exi('请先同步2级以上类目！','error');
	}
	$this->model('goods')->up_brand();
	exi('同步品牌成功！');
}